import { _decorator, Component, Node, tween, UITransform, v3 } from 'cc';
import { UIBase } from './UIBase';
import { XUtils } from '../comm/XUtils';
import { GameCtrl } from '../GameCtrl';
import { UIManager } from './UIManager';
import { GameData } from '../GameData';
import { GameStartCtrl } from './GameStartCtrl';
const { ccclass, property } = _decorator;

@ccclass('UIGameStart')
export class UIGameStart extends UIBase {

    @property(Node)
    private btnStart: Node;

    @property(Node)
    private btnContinue: Node;

    @property(Node)
    private btnLeft: Node;

    @property(Node)
    private btnRight: Node;

    @property(Node)
    private sw_content: Node;


    override onEnter(): void {
        super.onEnter();

        XUtils.bindButton(this.btnStart,this.clickStart,this);
        XUtils.bindButton(this.btnContinue,this.clickContinue,this);
        XUtils.bindButton(this.btnLeft,this.clickLeft,this);
        XUtils.bindButton(this.btnRight,this.clickRight,this);


        this.sw_content.setPosition(GameStartCtrl.Instance.currentLevel * -this.sw_content.getComponent(UITransform).contentSize.x,
            this.sw_content.position.y,this.sw_content.position.z) ;

        if(GameData.Instance.hasLevel(GameStartCtrl.Instance.currentLevel + 1)){
            this.btnContinue.active = true;
        }else{
            this.btnContinue.active = false;
        }

        if(GameStartCtrl.Instance.currentLevel == 0){
            this.btnLeft.active = false;
        }
        if(GameStartCtrl.Instance.currentLevel == 2){
            this.btnRight.active = false;
        }
    }

    override onExit(): void {
        super.onExit();

    }





    public clickStart(){
        UIManager.Instance.closeUI(UIManager.Instance.getUI('UIGameStart'));
        UIManager.Instance.openUI(UIManager.Instance.getUI('UILayer'));
        GameCtrl.Instance.startGame(GameStartCtrl.Instance.currentLevel + 1);
        console.log('Start new Game    level : ',GameStartCtrl.Instance.currentLevel);
    }

    public clickContinue(){
        UIManager.Instance.closeUI(UIManager.Instance.getUI('UIGameStart'));
        UIManager.Instance.openUI(UIManager.Instance.getUI('UILayer'));
        GameCtrl.Instance.continueGame(GameStartCtrl.Instance.currentLevel + 1);
        console.log('Continue Game    level : ',GameStartCtrl.Instance.currentLevel);
    }

    public clickLeft(){
        tween(this.sw_content).stop();
        GameStartCtrl.Instance.switchLevel(-1);
        if(GameStartCtrl.Instance.currentLevel == 0){
            this.btnLeft.active = false;
        }
        this.btnRight.active = true;

        tween(this.sw_content).to(0.2,{position: v3(
            GameStartCtrl.Instance.currentLevel * -this.sw_content.getComponent(UITransform).contentSize.x,
            this.sw_content.position.y,
            this.sw_content.position.z
        )}).start();

        if(GameData.Instance.hasLevel(GameStartCtrl.Instance.currentLevel + 1)){
            this.btnContinue.active = true;
        }else{
            this.btnContinue.active = false;
        }
    }

    public clickRight(){
        tween(this.sw_content).stop();
        GameStartCtrl.Instance.switchLevel(1);
        if(GameStartCtrl.Instance.currentLevel == 2){
            this.btnRight.active = false;
        }
        this.btnLeft.active = true;

        tween(this.sw_content).to(0.2,{position: v3(
            GameStartCtrl.Instance.currentLevel * -this.sw_content.getComponent(UITransform).contentSize.x,
            this.sw_content.position.y,
            this.sw_content.position.z
        )}).start();

        if(GameData.Instance.hasLevel(GameStartCtrl.Instance.currentLevel + 1)){
            this.btnContinue.active = true;
        }else{
            this.btnContinue.active = false;
        }
    }
}


